﻿//************************************************************************//
//文件功能：图像操作函数集合
//创 建 人：薄帅
//创建时间：2011年05月26日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using System.IO;

namespace ProjBackEnd.Common.Helper
{
    public class OperateImage
    {
        /// <summary>
        /// 捕捉屏幕图片
        /// </summary>
        /// <returns>Bitmap</returns>
        public static Bitmap CaptureScreen()
        {

            Graphics gScreen = Graphics.FromHwnd(IntPtr.Zero);//建立屏幕Graphics

            Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, gScreen);//根据屏幕大小建立位图
            Graphics gBitmap = Graphics.FromImage(bitmap);//建立位图相关Graphics
            gBitmap.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;

            IntPtr hdcScreen = gScreen.GetHdc();//建立屏幕上下文
            IntPtr hdcBitmap = gBitmap.GetHdc(); //建立位图上下文

            Api.BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcScreen, 0, 0, 13369376);//将屏幕捕获保存在图位中

            gBitmap.ReleaseHdc(hdcBitmap); //关闭位图句柄
            gScreen.ReleaseHdc(hdcScreen); //关闭屏幕句柄
            gBitmap.Dispose();//释放位图对像
            gScreen.Dispose(); //释放屏幕对像

            return bitmap;//返回捕获位图
        }
        /// <summary>
        /// 捕捉活动窗口图片
        /// </summary>
        /// <returns>Bitmap</returns>
        public static Bitmap CaptureActivedWindow()
        {
            IntPtr dcWindow = Api.GetActiveWindow();  //获取活动窗口句柄

            Api.RECT ActiveWindow = new Api.RECT();
            Api.GetWindowRect(dcWindow, out ActiveWindow);  //获取活动窗口矩形

            int width = ActiveWindow.Right - ActiveWindow.Left;
            int height = ActiveWindow.Bottom - ActiveWindow.Top;

            Graphics gWindow = Graphics.FromHwnd(dcWindow); //由一个指定设备的句柄创建一个新的Graphics对象

            Bitmap bitmap = new Bitmap(width, height, gWindow); //创建一个与之相同大小的Bitmap对象
            Graphics gBitmap = Graphics.FromImage(bitmap);  ////建立位图相关Graphics


            IntPtr hdcWindow = gWindow.GetHdc();    //获得活动窗口的句柄
            IntPtr hdcBitmap = gBitmap.GetHdc();    //获得位图的句柄

            //Api.BitBlt(hdcBitmap, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, hdcWindow, 0, 0, 13369376);
            Api.BitBlt(hdcBitmap, 0, 0, width, height, hdcWindow, 0, 0, 13369376);  //把当前活动窗口捕获到位图对象中

            gWindow.ReleaseHdc(hdcWindow);  //释放活动窗口句柄
            gBitmap.ReleaseHdc(hdcBitmap);  //释放位图句柄
            gWindow.Dispose();
            gBitmap.Dispose();

            return bitmap;
        }

        /// 格式化图片大小
        /// </summary>
        /// <param name="setImage">传入的图片</param>
        /// <param name="setWidth">格式化的宽度</param>
        /// <param name="setHeight">格式化的高度</param>
        /// <returns>格式化后的图片</returns>
        public static Bitmap SetPictureSize(Bitmap setImage, int setWidth, int setHeight)
        {
            if (setHeight <= 0 || setWidth <= 0)     //传入参数错误
            {
                return setImage;
            }

            int getHeigh;                               //获取的高
            int getWidth;                               //获取的宽
            int imageHeigh = setImage.Height;           //图片高度
            int imageWidth = setImage.Width;            //图片宽度
            decimal heightRate = imageHeigh / setHeight;    //高度比
            decimal widthRate = imageWidth / setWidth;      //宽度比
            if (heightRate <= 1 && widthRate <= 1)         //获取获得的高和宽
            {
                getHeigh = imageHeigh;
                getWidth = imageWidth;
            }
            else if (heightRate <= widthRate)
            {
                getHeigh = Convert.ToInt32(imageHeigh / widthRate);
                getWidth = Convert.ToInt32(imageWidth / widthRate);
            }
            else
            {
                getHeigh = Convert.ToInt32(imageHeigh / heightRate);
                getWidth = Convert.ToInt32(imageWidth / heightRate);
            }
            Bitmap retImage = new Bitmap(setImage, getWidth, getHeigh);
            return retImage;
        }

        /// <summary>
        /// 对一个指定的图片加上图片水印效果。
        /// </summary>
        /// <param name="imageFile">图片文件地址</param>
        /// <param name="waterImage">水印图片（Image对象）</param>
        public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
        {
            if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
            {
                return;
            }

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

            if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
            {
                return;
            }

            Graphics graphics = Graphics.FromImage(originalImage);

            int x = originalImage.Width - waterImage.Width - 10;
            int y = originalImage.Height - waterImage.Height - 10;
            int width = waterImage.Width;
            int height = waterImage.Height;

            graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
            graphics.Dispose();

            MemoryStream stream = new MemoryStream();
            originalImage.Save(stream, ImageFormat.Jpeg);
            originalImage.Dispose();

            System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

            imageWithWater.Save(imageFile);
            imageWithWater.Dispose();
        }
        /// <summary>
        /// 对一个指定的图片加上文字水印效果。
        /// </summary>
        /// <param name="imageFile">图片文件地址</param>
        /// <param name="waterText">水印文字内容</param>
        public static void CreateTextWaterMark(string imageFile, string waterText)
        {
            if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
            {
                return;
            }

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

            Graphics graphics = Graphics.FromImage(originalImage);

            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

            SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
            Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
            SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);

            float x = (float)originalImage.Width - waterTextSize.Width - 10F;
            float y = (float)originalImage.Height - waterTextSize.Height - 10F;

            graphics.DrawString(waterText, waterTextFont, brush, x, y);

            graphics.Dispose();
            brush.Dispose();

            MemoryStream stream = new MemoryStream();
            originalImage.Save(stream, ImageFormat.Jpeg);
            originalImage.Dispose();

            System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

            imageWithWater.Save(imageFile);
            imageWithWater.Dispose();
        }

    }
}
